import type { FormInstance, FormItemRule } from 'element-plus'

import type { DnsServerModel } from '@/apis/model/dns'
import { E_ALLOT_TYPE } from '@/config/enums/dns'

type IFrom = Pick<
  DnsServerModel,
  'domain' | 'addr' | 'ip' | 'remark' | 'assign_status'
>

export default function useForm() {
  /**
   * 表单 ref
   */
  const formRef = ref<FormInstance>()

  /**
   * 表单数据
   */
  const formData = reactive<IFrom>({
    // domain: '',
    addr: '',
    ip: '',
    remark: '',
    assign_status: E_ALLOT_TYPE.AUTO
  })

  /**
   * 表单验证规则
   */
  const formRules = reactive<{ [key in keyof IFrom]: FormItemRule[] }>({
    // domain: [
    //   { required: true, message: '请输入DNS服务器域名' },
    //   {
    //     validator: (rule, value, callback) =>
    //       formValidator(rule, value, callback, [domainValidate]),
    //     message: '域名格式错误'
    //   }
    // ],
    addr: [
      { required: true, message: '请输入DNS服务器地址' },
      {
        validator: (rule, value, callback) =>
          formValidator(rule, value, callback, [domainValidate]),
        message: 'DNS服务器地址无效'
      }
    ],
    ip: [
      { required: true, message: '请输入IP地址' },
      {
        validator: (rule, value, callback) =>
          formValidator(rule, value, callback, [ipValidate]),
        message: 'IPv4格式错误'
      }
    ]
  })

  return {
    formRef,
    formData,
    formRules
  }
}
